﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using ServiceStack.OrmLite;
using EquipmentManager.Dtos;

namespace EquipmentManager
{
    public class GlobalConfig
    {
        public static string SqliteDataBaseFile = Properties.Settings.Default.DataBaseFileName;
        public static string SqlServerAddress = Properties.Settings.Default.SqlServerAddress;

        public static IDbConnection DbConnection
        {
            get
            {
                if (Properties.Settings.Default.SelectDataBase == "sqlite")
                    return SqliteConnection;
                else if (Properties.Settings.Default.SelectDataBase == "sqlserver")
                    return SqlServerConnection;
                else
                    throw new Exception("数据库选择设置错误，要是sqlite 或者 sqlserver 或..");
            }
        }

        public static OrmLiteConnectionFactory _DbConnectionFactory;

        public static OrmLiteConnectionFactory DbConnectionFactory
        {
            get
            {
                /*if(_DbConnectionFactory == null)
                {
                    if (Properties.Settings.Default.SelectDataBase == "sqlite")
                        _DbConnectionFactory = new OrmLiteConnectionFactory(SqliteDataBaseFile, SqliteDialect.Provider);
                    else if (Properties.Settings.Default.SelectDataBase == "sqlserver")
                        _DbConnectionFactory = new OrmLiteConnectionFactory(SqlServerAddress, SqlServerDialect.Provider);
                    else
                        throw new Exception("数据库选择设置错误，要是sqlite 或者 sqlserver 或..");
                }*/
                return _DbConnectionFactory;

            }
        }
        private static IDbConnection _sqliteConnection;
        public static IDbConnection SqliteConnection
        {
            get
            {
                if (_sqliteConnection == null)
                {
                    _DbConnectionFactory = new OrmLiteConnectionFactory(SqliteDataBaseFile, SqliteDialect.Provider);
                    _sqliteConnection = _DbConnectionFactory.OpenDbConnection();
                }
                if (_sqliteConnection.State == ConnectionState.Broken)
                    _sqliteConnection.Close();
                if (_sqliteConnection.State == ConnectionState.Closed)
                    _sqliteConnection.Open();
                return _sqliteConnection;
            }
        }
        private static IDbConnection _sqlserverConnection;
        public static IDbConnection SqlServerConnection
        {
            get
            {
                if (_sqlserverConnection == null)
                {
                    _DbConnectionFactory = new OrmLiteConnectionFactory(SqlServerAddress, SqlServerDialect.Provider);
                    _sqlserverConnection = _DbConnectionFactory.OpenDbConnection();
                }
                if (_sqlserverConnection.State == ConnectionState.Broken)
                    _sqlserverConnection.Close();
                if (_sqlserverConnection.State == ConnectionState.Closed)
                    _sqlserverConnection.Open();
                return _sqlserverConnection;
            }
        }
        public static void CreateDataBase()
        {
            DbConnection.CreateTableIfNotExists<Equipment>();
            DbConnection.CreateTableIfNotExists<Todo>();
            if (!DbConnection.TableExists<User>())
            {
                DbConnection.CreateTable<User>();
                User user = new User
                {
                    Name = "Admin",
                    PassWord = "$1$B63KhP2W$n6gdWvlr8jmTQZYnf/v720",
                    PowerType = "root",
                };
                DbConnection.Save<User>(user);
            }
        }
    }
}
